﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace CoreLims.Models.CoreLims
{
    using LoginRelation = SampleLoginRelation;

    public static class SampleLoginRelationExtension
    {
        public static LoginRelation getLoginRelation(this LoginRelation lsample, CoreLimsContext dbContext, string Id) =>
            dbContext.LoginRelation.FirstOrDefault(s => s.Id == Id);

        /// <summary>
        /// 添加登录样品所包含的 分析项
        /// </summary>
        /// <param name="spdf"></param>
        /// <param name="dbContext"></param>
        /// <param name="SampleDefinitionId">样品定ID</param>
        /// <param name="AnalyteID">分析项ID</param>
        /// <returns></returns>
        public static string AddSampleLoginAanylte(this LoginRelation lr, CoreLimsContext dbContext, string SampleLoginId, string AnalyteID)
        {
            var rel = dbContext.LoginRelation.FirstOrDefault(s => s.SampleId == SampleLoginId && s.AnalyteId == AnalyteID
                && s.TestId == null);

            if (rel != null)
                return "样品已经包含此分析项！";

            rel = new LoginRelation();
            rel.Id = SAMPLE._newId();
            rel.SampleId = SampleLoginId;
            rel.AnalyteId = AnalyteID;
            rel.DetectionTimes = 1;

            dbContext.Add(rel);
            dbContext.SaveChanges();
            return "样品包含的分析项添加成功！";
        }

        /// <summary>
        /// 添加样品包含的指标集
        /// </summary>
        /// <param name="lr"></param>
        /// <param name="dbContext"></param>
        /// <param name="SampleLoginId"></param>
        /// <param name="SpecCollectionId"></param>
        /// <returns></returns>
        public static string AddSampleLoginSpecCollection(this LoginRelation lr, CoreLimsContext dbContext, string SampleLoginId, string SpecCollectionId)
        {
            var rel = dbContext.LoginRelation.FirstOrDefault(s => s.SampleId == SampleLoginId 
                && s.SpecCollectionId == SpecCollectionId && s.TestId == null);

            if (rel != null)
                return "样品已经包含该指标集！";

            var specrel = dbContext.LoginRelation.FirstOrDefault(s => s.SampleId == SampleLoginId
                && s.SpecCollectionId != null && s.TestId == null);

            rel = new LoginRelation();
            rel.Id = SAMPLE._newId();
            rel.SampleId = SampleLoginId;
            rel.SpecCollectionId = SpecCollectionId;
            rel.DetectionTimes = 1;
            if (specrel == null) // 第一个添加的指标集有默认值
                rel.IsDefault = true;

            dbContext.Add(rel);
            dbContext.SaveChanges();
            return "样品包含的指标集添加成功！";
        }

        public static string DeleteLoginRelation(this LoginRelation lr, CoreLimsContext dbContext, string Id)
        {
            var rel = dbContext.LoginRelation.FirstOrDefault(s => s.Id == Id);
            dbContext.LoginRelation.Remove(rel);
            dbContext.SaveChanges();
            return "关系项删除成功！";
        }

        /// <summary>
        /// 删除测试
        /// </summary>
        /// <param name="lr"></param>
        /// <param name="dbContext"></param>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static string DeleteLoginRelationTestDefinition(this LoginRelation lr, CoreLimsContext dbContext, string Id)
        {
            var rel = dbContext.LoginRelation.FirstOrDefault(s => s.Id == Id);
            var list = dbContext.LoginRelation.Where(s => s.TestId == rel.TestId && s.SampleId == rel.SampleId).ToList();

            foreach (var item in list)
                dbContext.LoginRelation.Remove(item);

            dbContext.SaveChanges();
            return "关系项删除成功！";
        }

        public static string UpdateIsDefault(this LoginRelation lr, CoreLimsContext dbContext, string SpecCollectionOrMethod, string Id)
        {
            var temp = SAMPLE.LoginRelation.getLoginRelation(dbContext, Id);

            if (SpecCollectionOrMethod == "SpecCollection")
            {
                var specList = dbContext.LoginRelation.Where(m => m.TestId == temp.TestId && m.SampleId == temp.SampleId && m.SpecCollectionId != null).ToList();
                SetFalse(dbContext, specList);
                temp.IsDefault = true;
                dbContext.Update(temp);
            }

            if (SpecCollectionOrMethod == "Method")
            {
                var methodList = dbContext.LoginRelation.Where(m => m.TestId == temp.TestId && m.SampleId == temp.SampleId && m.MethodId != null).ToList();
                SetFalse(dbContext, methodList);
                temp.IsDefault = true;
                dbContext.Update(temp);
            }

            if (SpecCollectionOrMethod == "SampleSpecCollection")  // 这个是样品直接关联的指标集
            {
                var specList = dbContext.LoginRelation.Where(m => m.SampleId == temp.SampleId && m.TestId == null && m.SpecCollectionId != null).ToList();
                SetFalse(dbContext, specList);
                temp.IsDefault = true;
                dbContext.Update(temp);
            }

            dbContext.SaveChanges();
            return "默认值更新成功！";
        }

        private static void SetFalse(CoreLimsContext dbContext, List<LoginRelation> specList)
        {
            foreach (var spec in specList)
            {
                spec.IsDefault = false;
                dbContext.Update(spec);
            }
        }
    }

}
